package cn.bdqn.controller;

import cn.bdqn.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class HelloThymeleafController {

    @GetMapping("/expression")
    public String expression(Model model, HttpServletRequest request, HttpSession session){
        model.addAttribute("msg","雷镇子");
        model.addAttribute("user",new User(1,"雷广清","男",17));

        request.setAttribute("username","许多");
        session.setAttribute("username","康建斌");

        model.addAttribute("date",new Date());
        return "demo/expression";
    }
    @GetMapping("/insert")
    @ResponseBody
    public User insert(User user){
        return user;
    }

    @GetMapping("/ifunelss")
    public String ifunelss(Model model){
        model.addAttribute("age",19);
        return "demo/ifunelss";
    }
    @GetMapping("/switch2")
    public String switch2(Model model){
        model.addAttribute("sex","女");
        return "demo/switch";
    }
    @GetMapping("/each")
    public String each(Model model){
        List<User> list=new ArrayList<>();
        list.add(new User(1,"张三","男",18));
        list.add(new User(2,"李四","男",19));
        list.add(new User(3,"王五","男",20));

        model.addAttribute("list",list);
        return "demo/each";
    }

    //内联
    @RequestMapping("/inline")
    public String inline(Model model){
        model.addAttribute("name","张三");
        return "demo/inline";
    }

    @RequestMapping("/include")
    public String include(){
        return "demo/frame";
    }

}
